//
//  RemindTableViewCell.m
//  SmartCup
//
//  Created by Kendama on 2017/5/18.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "RemindTableViewCell.h"
@interface RemindTableViewCell()

@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UIImageView *typeIV;
@property (nonatomic, strong) UIImageView *switchIV;
@property (nonatomic, strong) UILabel *timeLabel;

/** 开关按钮 **/
@property(nonatomic,strong) UIButton *switchButton;

@end

@implementation RemindTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self layoutSubviews];
    }
    return self;
}

- (void)setRemind:(RemindModel *)remind {
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    
    NSString *count = [NSString string];
    if (remind.count == 0) {
        count = @"自动";
    } else {
        count = [NSString stringWithFormat:@"%li次",(long)remind.count];
    }
    
    if ([remind.type isEqualToString:@"喝水"]) {
        self.typeIV.image = [UIImage imageNamed:@"bg_drink_water"];
        [string attributeStrings:@[@"喝水",[NSString stringWithFormat:@"（次数：%@）",count]] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]]]];
        
    } else if ([remind.type isEqualToString:@"吃药"]) {
        self.typeIV.image = [UIImage imageNamed:@"bg_medicine"];
        [string attributeStrings:@[@"吃药",[NSString stringWithFormat:@"（次数：%@）",count]] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]]]];
    }
    self.typeLabel.attributedText = string;
    
    NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] init];
    [string1 attributeStrings:@[remind.startTime,@" 至 ",remind.endTime] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]],@[[UIFont systemFontOfSize:px1080Width(64)]]]];
    self.timeLabel.attributedText = string1;
    
    
    self.switchButton.selected = remind.switchMark;

}


- (void)setDict:(NSDictionary *)dict {
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    
    NSString *count = [NSString string];
    if ([dict[@"count"] isEqualToString:@"0"]) {
        count = @"自动";
    } else {
        count = [NSString stringWithFormat:@"%@次",dict[@"count"]];
    }
    
    if ([dict[@"type"] isEqualToString:@"喝水"]) {
        self.typeIV.image = [UIImage imageNamed:@"bg_drink_water"];
        [string attributeStrings:@[@"喝水",[NSString stringWithFormat:@"（次数：%@）",count]] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]]]];

    } else if ([dict[@"type"] isEqualToString:@"吃药"]) {
        self.typeIV.image = [UIImage imageNamed:@"bg_medicine"];
        [string attributeStrings:@[@"吃药",[NSString stringWithFormat:@"（次数：%@）",count]] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]]]];
    }
    self.typeLabel.attributedText = string;

    NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] init];
    [string1 attributeStrings:@[dict[@"startTime"],@" 至 ",dict[@"endTime"]] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]],@[[UIFont systemFontOfSize:px1080Width(64)]]]];
    self.timeLabel.attributedText = string1;
    

    
    if ([dict[@"switch"] isEqualToString:@"YES"]) {
        self.switchButton.selected = YES;
    } else {
        self.switchButton.selected = NO;
    }
}



- (void)layoutSubviews {
    [self.typeIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(px1920Height(20));
        make.left.mas_equalTo(self).with.offset(px1080Width(20));
        make.right.mas_equalTo(self).with.offset(-px1080Width(20));
        make.bottom.mas_equalTo(self).with.offset(-px1920Height(20));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeIV);
        make.left.equalTo(self.typeIV).with.offset(px1080Width(20));
        make.right.equalTo(self.typeIV.mas_centerX);
        make.height.mas_equalTo(px1920Height(100));
    }];
    
    [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLabel.mas_bottom).with.offset(px1920Height(10));
        make.left.equalTo(self.typeIV).with.offset(px1080Width(20));
//        make.right.equalTo(self.typeIV.mas_centerX);
        make.width.mas_equalTo(px1080Width(140));
        make.height.mas_equalTo(px1920Height(80));
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.timeLabel.mas_top);
        make.left.equalTo(self.typeIV).with.offset(px1080Width(20));
        make.right.equalTo(self.typeIV.mas_centerX);
        make.height.mas_equalTo(px1920Height(120));
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor whiteColor];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeLabel.mas_bottom);
        make.left.equalTo(self.typeIV).with.offset(px1080Width(20));
        make.right.equalTo(self.typeIV.mas_centerX).with.offset(-px1080Width(100));
        make.height.mas_equalTo(px1920Height(2));

    }];
}

- (void)didClickSwitchButton {
    self.switchButton.selected = !self.switchButton.selected;
    if (self.switchBlock) {
        if (self.switchButton.selected) {
            self.switchBlock(YES);
        } else {
            self.switchBlock(NO);
        }
    }
}

- (UIImageView *)typeIV
{
    if(!_typeIV)
    {
        UIView *superView = self;
        _typeIV = [[UIImageView alloc]init];
        [superView addSubview:_typeIV];
        _typeIV.userInteractionEnabled = YES;
//        _typeIV.image = [UIImage imageNamed:@"bg_medicine"];//bg_drink_water
    }
    return _typeIV;
}


- (UIImageView *)switchIV
{
    if(!_switchIV)
    {
        UIView *superView = self.typeIV;
        _switchIV = [[UIImageView alloc]init];
        [superView addSubview:_switchIV];
        _switchIV.image = [UIImage imageNamed:@"button_on"];
        _switchIV.userInteractionEnabled = YES;

    }
    return _switchIV;
}

- (UIButton *)switchButton
{
    if(!_switchButton)
    {
        UIView *superView = self.typeIV;
        _switchButton = [[UIButton alloc]init];
        [superView addSubview:_switchButton];
        [_switchButton setBackgroundImage:[UIImage imageNamed:@"button_off"] forState:UIControlStateNormal];
        [_switchButton setBackgroundImage:[UIImage imageNamed:@"button_on"] forState:UIControlStateSelected];
        [_switchButton addTarget:self action:@selector(didClickSwitchButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchButton;
}



- (UILabel *)timeLabel
{
    if(!_timeLabel)
    {
        UIView *superView = self.typeIV;
        _timeLabel = [[UILabel alloc]init];
        [superView addSubview:_timeLabel];
        [_timeLabel setFont:[UIFont systemFontOfSize:15]];
        [_timeLabel setTextColor:[UIColor whiteColor]];


    }
    return _timeLabel;
}

- (UILabel *)typeLabel
{
    if(!_typeLabel)
    {
        UIView *superView = self.typeIV;
        _typeLabel = [[UILabel alloc]init];
        [superView addSubview:_typeLabel];
        [_typeLabel setFont:[UIFont systemFontOfSize:15]];
        [_typeLabel setTextColor:[UIColor whiteColor]];

    }
    return _typeLabel;
}



+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"RemindTableViewCell";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
